from lxml import etree
from lxml.builder import E
import logging
_logging = logging.getLogger(__name__)
xml = etree.parse('./ir_model_fields.xml')
# xml = etree.parse('./test.xml')
root = xml.getroot()


def generate_models_py(root):
    records = []
    models = {}
    for record in root:
        record_dict = {}
        for field in record:
            name = field.get('name')
            eval = field.get('eval')
            value = field.text
            if(eval != None):
                record_dict[name] = eval
            if(value != None):
                record_dict[name] = value
        model = record_dict['model']
        field_str = generate_field_py(record_dict)
        if model not in models:
            models[model] = []
            models[model].append(field_str)
        else:
            models[model].append(field_str)
        records.append(record_dict)
    # print(models)
    models_str = 'from odoo import models, fields, api, _\nfrom odoo.exceptions import UserError\nimport logging\n_logging = logging.getLogger(__name__)\n'
    for model in models:
        if 'x_' in model:
            model_tem = model.replace("x_", "einfo_")
            tem = model_tem.replace("_", ".")
            models_str += '\n\n\nclass %s(models.Model):\n    _name = "%s"\n' % (
                model_tem, tem)
        else:
            model_tem = model.replace(".", "_")
            models_str += '\n\n\nclass %s(models.Model):\n    _inherit = "%s"\n' % (
                model_tem, model)
        for field_str in models[model]:
            models_str += '    '+field_str + '\n'
        # print(models[model])
    print(models_str)
    return models_str


def generate_field_py(field):
    ttype = field['ttype']

    def get_name(field):
        name = field['name']
        name = name.replace("x_", "einfo_")
        return name
    if ttype == 'char':
        return get_name(field)+" = fields.Char(string=u'%s')" % field['field_description']
    elif ttype == 'boolean':
        return get_name(field)+" = fields.Boolean(string=u'%s')" % field['field_description']
    elif ttype == 'text':
        return get_name(field)+" = fields.Text(string=u'%s')" % field['field_description']
    elif ttype == 'html':
        return get_name(field)+" = fields.Html(string=u'%s')" % field['field_description']
    elif ttype == 'integer':
        return get_name(field)+" = fields.Integer(string=u'%s')" % field['field_description']
    elif ttype == 'float':
        return get_name(field)+" = fields.Float(string=u'%s')" % field['field_description']
    elif ttype == 'date':
        return get_name(field)+" = fields.Date(string=u'%s')" % field['field_description']
    elif ttype == 'dateTime':
        return get_name(field)+" = fields.DateTime(string=u'%s')" % field['field_description']
    elif ttype == 'binary':
        return get_name(field)+" = fields.Binary(string=u'%s')" % field['field_description']
    elif ttype == 'selection':
        return get_name(field)+" = fields.Selection(string=u'%s')" % field['field_description']
    elif ttype == 'reference':
        return get_name(field)+" = fields.Reference(string=u'%s')" % field['field_description']
    elif ttype == 'many2one':
        relation = field['relation']
        comodel_name = ''
        if 'x_' in relation:
            relation_tem = relation.replace("x_", "einfo_")
            comodel_name = relation_tem.replace("_", ".")
        else:
            comodel_name = relation
        return get_name(field)+" = fields.Many2one(string=u'%s',comodel_name='%s')" % (field['field_description'], comodel_name)
    elif ttype == 'one2many':
        relation = field['relation']
        relation_field = field['relation_field']
        comodel_name = ''
        inverse_name = ''
        if 'x_' in relation:
            relation_tem = relation.replace("x_", "einfo_")
            comodel_name = relation_tem.replace("_", ".")
        else:
            comodel_name = relation
        if 'x_' in relation_field:
            inverse_name = relation_field.replace("x_", "einfo_")
        else:
            inverse_name = relation_field
        return get_name(field)+" = fields.One2many(string=u'%s',comodel_name='%s', inverse_name='%s')" % (field['field_description'], comodel_name, inverse_name)
    elif ttype == 'many2many':
        relation = field['relation']
        comodel_name = ''
        if 'x_' in relation:
            relation_tem = relation.replace("x_", "einfo_")
            comodel_name = relation_tem.replace("_", ".")
        else:
            comodel_name = relation
        return get_name(field)+" = fields.Many2many(string=u'%s',comodel_name='%s')" % (field['field_description'], comodel_name)
    else:
        return ''


generate_models_py(root)
